(function () {
  window.Recorder = {
    swfObject: {},
    container: "recorderContainer",
    options: {},
    initialize: function (options) {
      this.options = options || {};

      if (!this.options.url) {
        console.error("未设置url");
        return;
      }

      this.container = this.options.container;
      this.initFlash();
    },
    initFlash: function () {
      const self = this;
      swfobject.embedSWF("flash/micSample.swf", Recorder.container, 668, 298, "10.0.0", undefined, undefined, {allowscriptaccess: "always"}, undefined, function (e) {
        setTimeout(function () {
          console.log('ref', e);
          self.swfObject = e.ref;
        }, 1000);
      });
    },
    record: function () {
      console.log('swfobject', this.flashInterface());
      this.flashInterface().record();
    },
    stopRecord: function () {
      console.log('swf', this.flashInterface());
      var base64 = this.flashInterface().stopRecord();
      console.log(base64);
      if (!base64) {
        return;
      }
      const xhr = new window.XMLHttpRequest();
      xhr.onreadystatechange = function (e) {
        if (e.currentTarget.status === 200 && e.currentTarget.readyState === 4) {
          if (Recorder.options.success) {
            Recorder.options.success(e.currentTarget.responseText);
          }
        } else {
          if (Recorder.options.error) {
            Recorder.options.error();
          }
        }
      };
      xhr.open('POST', Recorder.options.url, true);
      xhr.setRequestHeader("Content-type", "application/json");
      xhr.setRequestHeader('Accept', 'application/json');
      xhr.withCredentials = true;
      xhr.send(JSON.stringify({
        upload_file: base64,
        file_name: 'audio',
        content_type: 'audio/wav'
      }));
    },
    flashInterface: function () {
      return this.swfObject;
    },
    // actionscript delegate
    debug: function (msg) {
      console.log(msg);
    },
    upload: function () {
      var url = this.options.url;
      var audioParam = this.options.audioParam || "file";
      var params = this.options.params || {};
      console.log(url, audioParam, params);
      this.flashInterface().upload(url, audioParam, params);
    },
    uploadSuccess: function (responseText) {
      if (options.success) {
        options.success(this._externalInterfaceDecode(responseText));
      }
    },
    triggerEvent: function (eventName, arg0, arg1, arg2) {
      Recorder[eventName](arg0, arg1, arg2);
    },
    _executeInWindowContext: function (fn) {
      window.setTimeout(fn, 1);
    },
    _externalInterfaceDecode: function (data) {
      return data.replace(/%22/g, "\"").replace(/%5c/g, "\\").replace(/%26/g, "&").replace(/%25/g, "%");
    },
    authAllowCallback: function () {
      // 用户允许使用麦克风
      console.log(this.options);
      if (this.options.authAllowCallback) {
        this.options.authAllowCallback();
      }
      console.log('yes');
    },
    authRejectCallback: function () {
      // 用户拒绝使用麦克风
      console.log('no');
      if (this.options.authRejectCallback) {
        this.options.authRejectCallback();
      }
    },
    showFlashContainer: function () {
      // 需要显示flash窗口
      if (this.options.showFlashContainer) {
        this.options.showFlashContainer();
      }
      console.log('again');
    },
  };
}());
